package hf;

import java.io.File;

import org.eclipse.swt.graphics.Point;

import configuration.*;
import hf.SWTBrowser.SearchEngine;

public class RSConfig
{
	Configuration config;
	
	
	RSConfig(String configFile)
	{
		try
		{
			if (!new File(configFile).exists())
			{
				config = new Configuration();
				
				//	EMPTY(0), GOOGLE(1), SOUGOU(2), BAIDU(3), BING(4)
				config.setValue("SearchEngine","1");
				config.setValue("Homepage", "about:blank");
				config.setValue("Size", "Point {800, 600}");
				config.setValue("Maximized", "false");
				config.setValue("Location", "false");
			}
			else
				config = new Configuration(configFile);
		}
		catch (ConfigurationException e)
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	public SearchEngine getSearchEngine()
	{
		switch(Integer.valueOf(config.getValue("SearchEngine","1")))
		{
			case 1: return SearchEngine.GOOGLE;
			case 2: return SearchEngine.SOUGOU;
			case 3: return SearchEngine.BAIDU;
			case 4: return SearchEngine.BING;
		}
		return SearchEngine.NULL;
	}
	
	public Point getSize()
	{
		String s = config.getValue("Size", "Point {800, 600}");
		int x = Integer.valueOf(s.substring(s.indexOf("{")+1,s.indexOf(",")));
		int y = Integer.valueOf(s.substring(s.indexOf(",")+2,s.indexOf("}")));
		Point point = new Point(x ,y);
		
		return point;
	}

	public Point getLocation()
	{
		String s = config.getValue("Location", "Point {0, 0}");
		int x = Integer.valueOf(s.substring(s.indexOf("{")+1,s.indexOf(",")));
		int y = Integer.valueOf(s.substring(s.indexOf(",")+2,s.indexOf("}")));
		Point point = new Point(x ,y);
		
		return point;
	}

	public void setMaximized(boolean max)
	{
		config.setValue("Maximized", String.valueOf(max));
	}
	
	public boolean getMaximized()
	{
		return Boolean.valueOf(config.getValue("Maximized", "false"));
	}
	
	public void setSize(Point point)
	{
		config.setValue("Size", point.toString());
	}
	
	public void setLocation(Point point)
	{
		config.setValue("Location", point.toString());
	}
	
	public void setSearchEngine(SearchEngine se)
	{
		if( se == SearchEngine.GOOGLE)
		{
			config.setValue("SearchEngine", "1");
			return;
		}
		if( se ==  SearchEngine.SOUGOU)
		{
			config.setValue("SearchEngine", "2");
			return;
		}
		if( se == SearchEngine.BAIDU)
		{
			config.setValue("SearchEngine", "3");
			return;
		}
		if( se == SearchEngine.BING)
		{
			config.setValue("SearchEngine", "4");
			return;
		}
	}
	
	public String getHomepage()
	{
		return config.getValue("Homepage","about:blank");
	}

	public void save()
	{
		try
		{
			config.saveFile("system.conf", "Browser Configuration");
		}
		catch (ConfigurationException e)
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}
